using System;
using System.Diagnostics;
using AndyPike.Experiments.Fluent.Events;
using AndyPike.Experiments.Fluent.Interfaces;
using NUnit.Framework;

namespace AndyPike.Experiments.Fluent.Tests
{
    [TestFixture]
    public class FluentImageTestBase
    {
        private readonly Stopwatch stopwatch = new Stopwatch();
        private long operationStarted;
        protected IFluentImage image;

        [SetUp]
        public void SetUp()
        {
            image = new FluentImage(new Uri(@"C:\Open Source\AndyPike\Experiments\Fluent\src\AndyPike.Experiments.Fluent.Tests\Test-Input\Sunset.jpg"));
            image.CommandStarted += CommandStarted;
            image.CommandCompleted += CommandCompleted;
        }

        [TearDown]
        public void TearDown()
        {
            Console.WriteLine("---------------------------------------------------------------");
            stopwatch.Stop();
            stopwatch.Reset();
        }

        void CommandStarted(object sender, ImageCommandEventHandlerArgs args)
        {
            if (!stopwatch.IsRunning)
            {
                stopwatch.Start();
            }

            operationStarted = stopwatch.ElapsedMilliseconds;
        }

        void CommandCompleted(object sender, ImageCommandEventHandlerArgs args)
        {
            var elapsed = stopwatch.ElapsedMilliseconds;
            var duration = elapsed - operationStarted;

            Console.WriteLine("{0} took {1}ms (elapsed: {2})", args.CommandName, duration, elapsed);
        } 
    }
}